package org.tloss.web.editor.form.editors.jaxb;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public final class MapAdapter extends

XmlAdapter<FomMapType, Map<String, String>> {

	@Override
	public FomMapType marshal(Map<String, String> arg0) throws Exception {
		if (arg0 != null) {
			FomMapType myFomMapType = new FomMapType();
			for (Entry<String, String> entry : arg0.entrySet()) {
				MapEntryType myMapEntryType = new MapEntryType();
				myMapEntryType.key = entry.getKey();
				myMapEntryType.value = entry.getValue();
				myFomMapType.entry.add(myMapEntryType);
			}
			return myFomMapType;
		}
		return null;
	}

	@Override
	public Map<String, String> unmarshal(FomMapType arg0) throws Exception {
		if (arg0 != null) {
			HashMap<String, String> hashMap = new HashMap<String, String>();
			for (MapEntryType myEntryType : arg0.entry) {
				hashMap.put(myEntryType.key, myEntryType.value);
			}
			return hashMap;
		}
		return null;
	}

}